package com.findviewbydream.evil.eda.Utils.Dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.findviewbydream.evil.eda.R;
import com.findviewbydream.evil.eda.Utils.Utils;
import com.github.ybq.android.spinkit.SpinKitView;

/********************************************
 * 工程名：Eda
 * 包名：com.findviewbydream.evil.eda.Utils.Dialog
 * 时间：16-8-30 下午10:16
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：
 ********************************************/
public class LoadingDialog extends Dialog
{
    private static LoadingDialog dialog;
    private static View view;

    public static void showDialog(Context context)
    {
        dialog = new LoadingDialog(context, R.style.Dialog);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.loadingdialog, null);
        dialog.addContentView(view, new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);

        new Handler().postDelayed(new Runnable()
        {
            @Override
            public void run()
            {
                if (dialog != null)
                    if (dialog.isShowing())
                    {
                        Utils.Log("允许后退");
                        dialog.setCancelable(true);
                    }
            }
        }, 5000);

        dialog.show();
    }

    public static void showError(final Context context, final CallBack callBack)
    {
        if (dialog != null)
        {
            SpinKitView spin_kit = (SpinKitView) view.findViewById(R.id.spin_kit);
            CardView cardView = (CardView) view.findViewById(R.id.cardView);
            ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
            spin_kit.setVisibility(View.GONE);
            cardView.setVisibility(View.VISIBLE);

            Utils.Glide(R.drawable.broken, imageView);


            cardView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view)
                {
                    dismissDialog();
                    showDialog(context);
                    callBack.start();
                }
            });
        }
        else
            Log.e("错误", "加载窗口为空");
    }

    public static void dismissDialog()
    {
        if (dialog.isShowing())
        {
            dialog.dismiss();
            dialog = null;
        }
    }

    public LoadingDialog(Context context)
    {
        super(context);
    }

    public LoadingDialog(Context context, int themeResId)
    {
        super(context, themeResId);
    }

}

